<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>

<!--    卡片区域-->
    <el-card>
      <el-row :gutter="20">
        <el-col :span="9">
            <el-input placeholder="请输入内容" v-model="searchcontent" class="input-with-select" clearable @clear="searchgoods">
            <el-button slot="append" icon="el-icon-search" @click="searchgoods"></el-button>
          </el-input>
        </el-col>
        <el-col :span="6"><el-button type="primary" @click="addgoods">添加商品</el-button></el-col>
      </el-row>

<!--      渲染表格-->
      <el-table :data="goodsdata" stripe border>
        <el-table-column label="#" type="index"></el-table-column>
        <el-table-column label="商品名称" prop="goods_name" width="550px"></el-table-column>
        <el-table-column label="商品价格(元)"  prop="goods_price"></el-table-column>
        <el-table-column label="商品重量"  prop="goods_weight"></el-table-column>
        <el-table-column label="创建时间"  prop="add_time">
          <template slot-scope="scope">{{scope.row.add_time | dataFormat}}</template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" size="mini"></el-button>
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="deletegoods(scope.row)"></el-button>
          </template>
        </el-table-column>
      </el-table>

<!--      分页功能-->
      <el-pagination
        @size-change="goodshandleSizeChange"
        @current-change="goodshandleCurrentChange"
        :current-page="goodsdataquery.pagenum"
        :page-sizes="[1, 5, 10, 20]"
        :page-size="goodsdataquery.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 搜索输入框的数据绑定
      searchcontent: '',
      // 定义获取商品列表数据的参数
      goodsdataquery: {
        query: '',  // 查询参数
        pagenum: 1, // 当前的页码
        pagesize: 5 // 每页显示条数
      },
      total: '',  // 商品总数
      // 定义商品数据
      goodsdata: []

    }
  },
  created() {
    // 调用获取商品列表数据函数
    this.getgoodslist()
  },
  methods: {
    async getgoodslist() {
      const {data: res} = await this.$http.get(`goods`,{
        params: this.goodsdataquery
      })
      if (res.meta.status !== 200) return this.$message.error('获取商品列表数据失败')
      console.log(res.data.goods)
      this.goodsdata = res.data.goods
      this.goodsdataquery.pagenum = res.data.pagenum  // 更新页码
      this.total = res.data.total  // 更新总数据条数
    },
    goodshandleSizeChange(pagesize) {  // 当pagesize发送改变
      this.goodsdataquery.pagesize = pagesize
      this.getgoodslist()

    },
    goodshandleCurrentChange(pagenum) {   // 当pagenum发送改变
      this.goodsdataquery.pagenum = pagenum
      this.getgoodslist()
    },
    async deletegoods(row) {
      console.log(row.goods_id)
      const results = await this.$confirm('此操作将永久删除该商品, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
      if (results !== 'confirm') return false
      const {data: res} = await this.$http.delete(`goods/${row.goods_id}`)
      if (res.meta.status !== 200) return this.$message.error('删除失败')
      this.$message.success('删除成功')
      this.getgoodslist()
    },
    searchgoods() {
      this.goodsdataquery.query = this.searchcontent
      this.getgoodslist()
      this.goodsdataquery.query = ''
    },
    addgoods() {
      this.$router.push('/goods/add')
    }

  }
}
</script>

<style scoped>
.el-breadcrumb {
  margin: 0 0 15px 15px;
}
.el-pagination {
  margin-top: 10px;
}
.el-cascader {
  margin-left: 15px;
}
.el-table {
  margin-top: 20px;
}
</style>
